package site.zhouinfo.function;

/**
 * Java8 函数特性
 *
 * @author zhou
 * @create 2017-02-08 16:11
 * @email zhouinfo@qq.com
 */
public class HigherOrderFunction {

    public static void main(String[] args) {
        System.out.println(getIdentity(1, 10));
        System.out.println(getSquare(1, 10));
        System.out.println(getCube(1, 10));
    }

    public static int getIdentity(int a, int b) {
        HighFunInterface identityFun = x -> x;
        return sum(identityFun, a, b);
    }

    public static int getSquare(int a, int b) {
        HighFunInterface squareFun = x -> x * x;
        return sum(squareFun, a, b);
    }

    public static int getCube(int a, int b) {
        HighFunInterface cubeFun = x -> x * x * x;
        return sum(cubeFun, a, b);
    }

    public static int sum(HighFunInterface fun, int a, int b) {
        int sum = 0;
        for (int i = a; i <= b; i++) {
            sum += fun.opera(i);
        }
        return sum;
    }
}
